import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Query,
  Post,
  Put,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { IsAuth } from 'src/decorators/auth.decorator';
import { IResponseData, responseData } from 'src/utils/result';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { BillingManagementService } from './billingManagement.service';
import {
  CreateBillingManagementDto,
  FindBillingManagementListDto,
  BillingManagementDto,
  UpdateBillingManagementDto,
} from './billingManagement.dto';
import { Between } from 'typeorm';

@ApiTags('账单模块')
@ApiBearerAuth()
@Controller('billingManagement')
export class BillingManagementController {
  constructor(
    private readonly BillingManagementService: BillingManagementService,
  ) {}
  @ApiOperation({ summary: '查询账单列表' })
  @ApiResponse({
    status: 200,
    description: '查询账单列表',
    type: BillingManagementDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  @IsAuth('system:billingManagement:list')
  async findList(
    @Query() dto: FindBillingManagementListDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const query = { ...dto };
    delete query.time;
    let date_time;
    if (dto.time && dto.time.length) {
      date_time = Between(dto.time[0], dto.time[1]);
    }
    const res = await this.BillingManagementService.getLists({
      where: {
        ...query,
        date_time,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询所有账单' })
  @ApiResponse({
    status: 200,
    description: '查询所有账单',
    type: BillingManagementDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get('findAll')
  async findAll(@Headers() headers): Promise<IResponseData> {
    const res = await this.BillingManagementService.getAll();
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询账单详情' })
  @ApiResponse({
    status: 200,
    description: '查询账单详情',
    type: BillingManagementDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(':id')
  @IsAuth('system:billingManagement:detail')
  async find(
    @Param('id') id: number,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.BillingManagementService.getOne({
      where: {
        id,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @Post()
  @IsAuth('system:billingManagement:add')
  @ApiOperation({ summary: '创建账单' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async create(
    @Body() dto: CreateBillingManagementDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.BillingManagementService.create(dto);
    return responseData({
      code: 100020,
      data: res,
      language: headers['language'],
    });
  }

  @Put()
  @IsAuth('system:billingManagement:update')
  @ApiOperation({ summary: '更新账单' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async update(
    @Body() dto: UpdateBillingManagementDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.BillingManagementService.update(dto);
    return responseData({
      code: 100030,
      data: res,
      language: headers['language'],
    });
  }

  @Delete()
  @IsAuth('system:billingManagement:delete')
  @ApiOperation({ summary: '删除账单' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async delete(
    @Query('ids') query: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.BillingManagementService.delete(query);
    return responseData({
      code: 100040,
      data: res,
      language: headers['language'],
    });
  }
}
